package telepasea.app;

import android.app.Activity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.view.View.OnClickListener; 

public class PushFilterUI extends Activity {
	private static String PF_ONOFF;
	private static String PF_M;
	private static String PF_F;
	private static String PF_ACT1;
	private static String PF_ACT2;
	private static String PF_ACT3;
	private static String PF_ACT4;
	private static String PF_ACT5;
	private static ListView itemlist;
	Button button1;
	CheckBox checkbox1; //Man
	CheckBox checkbox2; //Woman
	CheckBox checkbox3; //OnOff
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pushfilter);

		itemlist = (ListView) findViewById(R.id.listView3);

		// ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
		// this, R.array.ActivityList,
		// android.R.layout.simple_list_item_multiple_choice);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.ActivityList, R.layout.list_multiple_choice);
		itemlist.setAdapter(adapter);
		itemlist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		readPushFilter();
		
		button1 = (Button) findViewById(R.id.button1);
		button1.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				writePushFilter();
				finish();
			}
		});
	}
	
	public void readPushFilter(){
		checkbox1 = (CheckBox) findViewById(R.id.checkBox1);
		checkbox2 = (CheckBox) findViewById(R.id.checkBox2);
		checkbox3 = (CheckBox) findViewById(R.id.checkBox3);
		
		ListView itemlist = (ListView) findViewById(R.id.listView3);

		PF_ONOFF = Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "PF_ONOFF");
		PF_M = Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "PF_M");
		PF_F = Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "PF_F");
		PF_ACT1 = Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "PF_ACT1");
		PF_ACT2 = Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "PF_ACT2");
		PF_ACT3 = Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "PF_ACT3");
		PF_ACT4 = Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "PF_ACT4");
		PF_ACT5 = Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "PF_ACT5");

		if(PF_ONOFF.equals("checked"))		checkbox3.setChecked(true);
		if(PF_M.equals("checked"))			checkbox1.setChecked(true);
		if(PF_F.equals("checked"))			checkbox2.setChecked(true);
		if(PF_ACT1.equals("checked")){
			itemlist.setItemChecked(0, true);
		}
		if(PF_ACT2.equals("checked")){
			itemlist.setItemChecked(1, true);
		}
		if(PF_ACT3.equals("checked")){
			itemlist.setItemChecked(2, true);
		}
		if(PF_ACT4.equals("checked")){
			itemlist.setItemChecked(3, true);
		}
		if(PF_ACT5.equals("checked")){
			itemlist.setItemChecked(4, true);
		}
	}
	
	public void writePushFilter(){
		if(checkbox3.isChecked())	Util.setSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "PF_ONOFF", "checked");
		else	Util.setSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "PF_ONOFF", "");
		
		if(checkbox1.isChecked())	Util.setSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "PF_M", "checked");
		else	Util.setSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "PF_M", "");
		
		if(checkbox2.isChecked())	Util.setSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "PF_F", "checked");
		else	Util.setSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "PF_F", "");
		
		SparseBooleanArray sb = itemlist.getCheckedItemPositions();
		
		if(sb.size()!=0)
		{
			for(int i=itemlist.getCount()-1;i >=0 ; i--){
				int j=i+1;
				if(sb.get(i)){		
					Util.setSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "PF_ACT"+j, "checked");
				}
				else
				{
					Util.setSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "PF_ACT"+j, "");
				}
				
			}			
		}
	}
	
	public String read_PF_ONOFF(){
		return Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "PF_ONOFF");
	}
	
	public String read_PF_M(){
		return Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "PF_M");
	}
	
	public String read_PF_F(){
		return Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "PF_F");
	}
	
	public String read_PF_ACT(int pos){
		return Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", MODE_PRIVATE, "PF_ACT" + pos);
	}
}
